import { createRouter, createWebHistory } from 'vue-router'

interface RouteMeta extends Record<string, unknown> {
  index: number;
  title: string;
  requiresAuth?: boolean;
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: () => import('../views/Home.vue'),
      meta: {
        index: 0,
        title: '首页 - 旭佰塑业'
      }
    },
    {
      path: '/about',
      name: 'About',
      component: () => import('../views/About.vue'),
      meta: {
        index: 1,
        title: '关于我们 - 旭佰塑业'
      }
    },
    {
      path: '/products',
      name: 'Products',
      component: () => import('../views/Products.vue'),
      meta: {
        index: 2,
        title: '产品中心 - 旭佰塑业'
      }
    },
    {
      path: import.meta.env.BASE_URL + '/history',
      name: 'History',
      component: () => import('../views/History.vue'),
      meta: {
        index: 3,
        title: '发展历程 - 旭佰塑业'
      }
    },
    {
      path: '/contact',
      name: 'Contact',
      component: () => import('../views/Contact.vue'),
      meta: {
        index: 4,
        title: '联系我们 - 旭佰塑业'
      }
    },


  ]
})

// 路由导航守卫
router.beforeEach((to, _from, next) => {
  // 修改页面标题
  const meta = to.meta as unknown as RouteMeta;
  if (meta.title) {
    document.title = meta.title;
  }

  next();
});

export default router
